package net.zjitc.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.zjitc.common.ResponseResult;
import net.zjitc.entity.Company;
import net.zjitc.entity.Mycompany;
import net.zjitc.service.IMycompanyService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

/**
 * @auther 郭梦婷
 * @create 2022-05-30 21:34:03
 * @describe 前端控制器
 */
@CrossOrigin
@RestController
@Api(tags = "用户关注的公司")
public class MycompanyController {
    @Reference
    private IMycompanyService mycompanyService;

    ResponseResult result=new ResponseResult<>();

    @ApiOperation("关注")
    @GetMapping("AddAttention")
    public ResponseResult AddAttention(@RequestParam(required = false) String companyid,
                                       @RequestParam(required = false) String username){
        if (companyid.length()!=0&&username.length()!=0){

            Mycompany mycompany=new Mycompany();
            mycompany.setCompanyid(companyid);
            mycompany.setUsername(username);
            boolean save = mycompanyService.save(mycompany);
            if (save){
                result.setSuccess("添加关注成功");
            }else {
                result.setError("添加关注失败");
            }
        }else {
            result.setError("没有数据");
        }
        return result;

    }
    @ApiOperation("关注")
    @GetMapping("IfAttention")
    public ResponseResult IfAttention(@RequestParam(required = false) String companyid,
                                      @RequestParam(required = false) String username){
        Mycompany mycompany= mycompanyService.IfAttention(companyid,username);
        if (mycompany!=null){
            result.setSuccess("已经关注",mycompany);
        }else {
            result.setError("未关注");
        }
        return result;
    }

    @ApiOperation("关注")
    @GetMapping("DeleteAttention")
    public ResponseResult DeleteAttention(@RequestParam(required = false) String companyid,
                                      @RequestParam(required = false) String username){
        int delete= mycompanyService.DeleteAttention(companyid,username);
        if (delete!=0){
            result.setSuccess("取关成功",delete);
        }else {
            result.setError("取关失败");
        }
        return result;
    }
    @ApiOperation("查看我的关注")
    @GetMapping("getAllAttentionByUsername")
    public ResponseResult getAllAttentionByUsername(@RequestParam(required = true) String username,
                                                    @RequestParam(required = true ,value = "pagenum") Integer pagenum,
                                                    @RequestParam(required = true,value = "pagesize") Integer pagesize){
        Page<Company> companyPage = mycompanyService.getAllAttentionByUsername(username, pagenum, pagesize);
        if (companyPage.getTotal()!=0){
            result.setSuccess("获取成功",companyPage);
        }else {
            result.setError("获取失败");
        }
        return result;
    }




}

